package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuDetailService spuDetailService;
    /**
     * 3-1 分页查询spu
     * @param page
     * @param rows
     * @param saleable
     * @param categoryId
     * @param brandId
     * @param id
     * @return
     */
//
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>>querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ){
        return ResponseEntity.ok(this.spuService.querySpuByPage(page,rows,saleable,categoryId,brandId,id));
    }

    /**
     * 3-2 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>addGoods(@RequestBody SpuDTO spuDTO){
        this.skuService.addGoods(spuDTO);
        return ResponseEntity.status(201).build();
    }

    /**
     *
     *
     * @param spuDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void>updatagoods(@RequestBody SpuDTO spuDTO){
        this.skuService.updatagoods(spuDTO);
        return ResponseEntity.status(204).build();
    }
    /**
     *3-4 根据id批量查询sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>>listSkuByIds(@RequestParam("id") List<Long> ids){
        return ResponseEntity.ok(this.skuService.listSkuByIds(ids));
    }

    /**
     *3-5 根据spuID查询sku的集合
     * @param spuID
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>skuBySpuId(@RequestParam("id") Long spuID){
        return ResponseEntity.ok(this.skuService.skuBySpuId(spuID));
    }

    /**
     * 3-6根据id查询商品详情
     * @param spuID
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO>queryGoods(@RequestParam("id") Long spuID){
        return ResponseEntity.ok(this.spuService.queryGoods(spuID));
    }
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id, @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(spuDetailService.querySpecValues(id, searching));
    }


    /**
     *3-8 修改商品上下架
     * @param id 商品spu的id
     * @param saleable true：上架；false：下架
     * @return 无
     */
    @PutMapping("saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }



    /**
     * 3-9根据id查询商品
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }

    /**
     * 3-10根据id查询商品
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){


        return ResponseEntity.ok(this.spuService.querySpuById(id));
    }
}
